<?php
class News_model extends CI_Model
{
	function __construct()
    {
        parent::__construct();
    }
	
	// Begin List News
	function get_all_news($num,$offset,$arr_search=array())
	{
		/*Begin search*/
		if(isset($arr_search['key_search'])&&$arr_search['key_search']!='')
		{
			if($arr_search['field_search']=='TT')
			{
				$this->db->like('news.lb_title',$arr_search['key_search']);
			}
		}
		/*End search*/
		$this->db->where('(bl_active=1 OR bl_active=0)');
		$this->db->order_by('news_id','DESC');
		$query = $this->db->get('news',$num,$offset);
		return $query->result();
	}
	
	function get_num_news($arr_search=array())
	{
        /*Begin search*/
        if(isset($arr_search['key_search'])&&$arr_search['key_search']!=''){
            if($arr_search['field_search']=='TT'){
                $this->db->like('news.lb_title',$arr_search['key_search']);
            }
        }
		/*End search*/
		$this->db->where('(bl_active=1 OR bl_active=0)');
		$query = $this->db->get('news');
		return $query->num_rows();
	}
	
	function get_list_category_news()
	{
		 return $this->db->get('news_category')->result();
	}  
	 
	//Save news
	function save_news($data)
	{
		 $id = (int)$this->uri->segment(3);
		 if($id!=0){
			  $this->db->where('news_id',$id);
			  if($this->db->update('news',$data)){
					return true;
			  }else{
					return false;
			  }
		 }else{
			  if($this->db->insert('news',$data)){
					return true;
			  }else{
					return false;
			  }
		 }
	}
	
	function del_updates($id)
	{
		$data['bl_active'] = -1;
		$data['dt_create'] = date("Y-m-d H:i:s") ;
		$this->db->where('news_id',$id);
		if($this->db->update('news',$data))
		{
			return true;
		  }else{
			return false;
		  }
	}
	
	function remove()
	{
		$date = new DateTime();
		$date->modify('-3 month');
		$date_del = $date->format('Y-m-d H:i:s');
		
		$this->db->where('bl_active', -1);
		$this->db->where('dt_create <', $date_del);
		if($this->db->delete('news')){
		  	return true;
		}else{
		  	return false;
		}
	}
	//Update News
	function get_news_item()
	{
		 $id = (int)$this->uri->segment(3);
		 $this->db->where('news_id',$id);
		 $query = $this->db->get('news');
		 return $query->row();
	}
	
	/*function get_parent()
	{
		 return $this->db->get('news_category')->result();
	}*/
	
	function get_news_by_listid($arr_id)
	{
		$this->db->where_in('news_id', $arr_id);
		return $this->db->get('news')->result();
	}
	
	// trash
	function get_all_trash($num,$offset,$arr_search=array())
	{
        /*Begin search*/
        if(isset($arr_search['key_search'])&&$arr_search['key_search']!='')
		{
            if($arr_search['field_search']=='TT')
			{
                $this->db->like('news.lb_title',$arr_search['key_search']);
            }
        }
		/*End search*/
		$this->db->where('bl_active', -1);
		$this->db->order_by('news_id','DESC');
		$query = $this->db->get('news',$num,$offset);
		return $query->result();
	}
	
	function get_num_trash($arr_search=array())
	{
        /*Begin search*/
        if(isset($arr_search['key_search'])&&$arr_search['key_search']!='')
		{
            if($arr_search['field_search']=='TT')
			{
                $this->db->like('news.lb_title',$arr_search['key_search']);
            }
        }
		/*End search*/
		$this->db->where('bl_active', -1);
		$query = $this->db->get('news');
		return $query->num_rows();
	}
	
	function del_news($id)
	{
		 $this->db->select('news_id,lb_image');
		 $this->db->where('news_id',$id);
		 $check = $this->db->get('news')->row();
		 if($check)
		 {
			  if(file_exists('../uploads/images/'.$check->lb_image))
			  {
					//unlink('../uploads/images/'.$check->lb_image);
			  }
			  $this->db->where('news_id',$id);
			  if($this->db->delete('news'))
			  {
					return true;
			  }else{
					return false;
			  }
		 }
		 else
		 {
			  return false;
		 }
	}
	
	// news_category
	function getAllCat($num=50,$offset=0)
	{
		//$this->db->where('nb_level',0);
		//$this->db->where('parent_id',0);
		$query = $this->db->get('news_category',$num,$offset);
		return $query->result();
	}
	
	function getNumCat()
	{
		//$this->db->where('nb_level',0);
		//$this->db->where('parent_id',0);
		$query = $this->db->get('news_category');
		return $query->num_rows();
	}
	
	function getCatActive($num=50,$offset=0)
	{
		$this->db->where('bl_active',1);
		$query = $this->db->get('news_category',$num,$offset);
		return $query->result();
	}
	
	function getNumCatActive()
	{
		$this->db->where('bl_active',1);
		$query = $this->db->get('news_category');
		return $query->num_rows();
	}
	
	function getItemCat()
	{
		$category_id = $this->uri->segment(3);
		$this->db->where('nb_level',0);
		$this->db->where('category_id',$category_id);
		$query = $this->db->get('news_category');
		return $query->row();
	}
	
	function get_Parent($category_id)
	{
	  $this->db->where('nb_level',0);
	  $this->db->where('parent_id',$category_id);
	  return $this->db->get('news_category')->result();
	}
	
	function saveUpdateCat()
	{
	  $id = (int)$this->uri->segment(3);
	  $lb_category = $this->input->post('lb_category');
	  $parent_id  = $this->input->post('parent_id');
	  $bl_active = $this->input->post('bl_active');
	  $data = array(
		'lb_category' => $lb_category,
		'parent_id' => $parent_id,
		'nb_level' => 0,
		'bl_active' => $bl_active
	  );
	  if($id != 0){
		  $this->db->where('category_id',$id);
		  if($this->db->update('news_category',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }else{
	
		  if($this->db->insert('news_category',$data)){
			  return true;
		  }else{
			  return false;
		  }
	  }          
	}
	
	function delcat($id)
	{
	  $this->db->where('nb_level',0);
	  $this->db->where('category_id',$id);
	  if($this->db->delete('news_category')){
		  return true;
	  }else{
		  return false;
	  }
	}
      
}
?>
